package com.zg.ktcomwan.fragment

import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.widget.FrameLayout
import androidx.fragment.app.FragmentManager
import com.jeremyliao.liveeventbus.LiveEventBus
import com.just.agentweb.AgentWeb
import com.just.agentweb.WebChromeClient
import com.just.agentweb.WebViewClient
import com.zg.core.ext.logI
import com.zg.ktcomwan.R
import com.zg.ktcomwan.base.BaseFragment
import kotlinx.android.synthetic.main.common_tbar.*
import kotlinx.android.synthetic.main.fragment_web.*


/***
 * @Author:      zxd
 * @CreateDate:  2021/8/24 16:29
 * @Description: web碎片
 * 1.如果标题和返回按钮都是在碎片中的话，那么进入网页碎片的时候需要把 首页的标题和底部导航栏都隐藏。
 * 进入这个界面的时候需要发消息，隐藏控件。从这个页面退出也需要发消息，显示控件
 * 2.如果标题和返回按钮都是在主界面中的话，标题在切换的过程中，应该是可以得到的。并且需要隐藏底部导航栏和控制返回按钮的显示和隐藏
 */
open class WebFragment : BaseFragment() {

    lateinit var url: String
    lateinit var title: String
    lateinit var agentWeb: AgentWeb

    override fun getLayoutId(): Int {
        return R.layout.fragment_web
    }

    override fun afterCreate() {
        //获取传过来的值，并且展示页面
        url = arguments?.getString("url").toString()
        title = arguments?.getString("title").toString()
        common_title.text = title
        iv_arrow.visibility = View.VISIBLE
        iv_arrow.setOnClickListener {
//            val navController =
//                Navigation.findNavController(requireActivity(), R.id.nav_host_fragment)
//            navController.currentBackStackEntry

            logI("${childFragmentManager.backStackEntryCount}")
            logI("${parentFragmentManager.backStackEntryCount}")
            logI("${fragmentManager?.backStackEntryCount}")
            logI("${requireFragmentManager().backStackEntryCount}")

            val a =
                parentFragmentManager.getBackStackEntryAt(parentFragmentManager.backStackEntryCount - 1)
            logI("**********${a.name}")
            parentFragmentManager.popBackStack(
                "${a.name}",
                FragmentManager.POP_BACK_STACK_INCLUSIVE
            )

        }
        logI("url===${url}")

        val webViewClient: WebViewClient =
            object : WebViewClient() {
                override fun onPageFinished(view: WebView, url: String) {
                    super.onPageFinished(view, url)
                    val title = view.title //拿到webView的title
                    if (!TextUtils.isEmpty(title)) {
                        //从这里调用首页的 标题的变化
                        //logI("onPageFinished:${title}")
                        //1.通过接口的方式，返回数据
                        //2.通过livedatabus，发送数据和监听数据
                        //LiveEventBus.get<String>("textChange").post(title)
                    }
                }
            }

        val webChromeClient: WebChromeClient =
            object : WebChromeClient() {
                override fun onReceivedTitle(view: WebView?, title: String?) {
                    super.onReceivedTitle(view, title)
                    if (!TextUtils.isEmpty(title)) {
                        logI("onReceivedTitle:${title}")
                        LiveEventBus.get<String>("textChange").post(title)
                    }
                }
            }

        agentWeb = AgentWeb.with(this)
            .setAgentWebParent(webview.parent as ViewGroup, FrameLayout.LayoutParams(-1, -1))
            .useDefaultIndicator()
            .setWebChromeClient(webChromeClient)
            .setWebViewClient(webViewClient)
            .createAgentWeb()
            .ready()
            .go(url);
    }

    override fun onPause() {
        agentWeb.webLifeCycle.onPause()
        super.onPause()
    }

    override fun onResume() {
        agentWeb.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onDestroyView() {
        agentWeb.webLifeCycle.onDestroy()
        super.onDestroyView()
    }
}